from django.shortcuts import render
from django.http import HttpResponse
import re

class LoginMiddleware:
	def __init__(self,get_response):
		self.get_response = get_response

	def __call__(self,request):
		# 检测当前的请求是否已登录，若已登录，则放行；如未登录，则跳转到登陆页
		
		# 自定义允许的请求路径
		urllist = ['/admin/login/','/admin/getvcode/']

		# 判断是否要进入后台
		if re.match('/myorder/',request.path) or re.match('/member/',request.path) or re.match('/order/',request.path) or re.match('/useredit/',request.path) and request.path not in urllist:
			# 检测session中是否存在 adminlogin的数据记录
			if not request.session.get('VipUser',None):
				# 如果session没有记录，则证明没有登录，转到登陆页面
				return HttpResponse('<script>alert("请先登陆");location.href="/login"</script>')
		
		response = self.get_response(request)
		return response	